package com.lin.execute;

import com.lin.entity.Prop;
import com.lin.info.PropInfo;
import com.lin.info.PropInfoList;
import com.net.Network;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.logging.Level;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

public class PropUtil {
    public static void save(Prop prop){
        final boolean[] wait = {true};
        Network network = new Network();
        network.saveProp(new Network.Callback() {
            @Override
            public void onResult(Object object) {
                wait[0] = false;
            }

            @Override
            public void onError(String error) {
                wait[0] = false;
            }
        }, prop);

        while (wait[0]) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static PropInfoList propInfoList(String filePath) {
        PropInfoList propInfoList = new PropInfoList();
        if (filePath != null) {
            try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(new File(filePath));
                doc.getDocumentElement().normalize();
                // 修改为解析 <PropInfo> 节点
                NodeList itemNodes = doc.getElementsByTagName("PropInfo");
                for (int i = 0; i < itemNodes.getLength(); i++) {
                    Prop prop = new Prop();
                    Element itemElement = (Element) itemNodes.item(i);
                    // 获取 <Matrix> 节点
                    Element matrixElement = (Element) itemElement.getElementsByTagName("Matrix").item(0);

                    // 解析图片
                    NodeList imageStringNodes = matrixElement.getElementsByTagName("ImageString");
                    if (imageStringNodes.getLength() > 0) {
                        Element imageStringElement = (Element) imageStringNodes.item(0);
                        String base64Image = imageStringElement.getTextContent();
                        if (base64Image != null) {
                            prop.setImageString(base64Image);
                        } else {
                            LOGGER.warning("No ImageString found at index " + i);
                        }
                    }

                    // 解析名字
                    NodeList nameNodes = matrixElement.getElementsByTagName("Name");
                    if (nameNodes.getLength() > 0) {
                        Element nameElement = (Element) nameNodes.item(0);
                        prop.setName(nameElement.getTextContent());
                    } else {
                        prop.setName("");
                    }

                    // 解析位置
                    NodeList locationNodes = matrixElement.getElementsByTagName("Position");
                    if (locationNodes.getLength() > 0) {
                        Element locationElement = (Element) locationNodes.item(0);
                        if (!locationElement.getTextContent().isEmpty())
                            prop.setPosition(Integer.parseInt(locationElement.getTextContent()));
                    } else {
                        prop.setPosition(0);
                    }

                    // 解析作用
                    NodeList functionNodes = matrixElement.getElementsByTagName("Function");
                    if (functionNodes.getLength() > 0) {
                        Element functionElement = (Element) functionNodes.item(0);
                        prop.setFunction(functionElement.getTextContent());
                    } else {
                        prop.setFunction("");
                    }

                    // 解析备注
                    NodeList descriptionNodes = matrixElement.getElementsByTagName("Description");
                    if (descriptionNodes.getLength() > 0) {
                        Element descriptionElement = (Element) descriptionNodes.item(0);
                        prop.setDescription(descriptionElement.getTextContent());
                    } else {
                        prop.setDescription("");
                    }
                    // 新增：解析类型
                    NodeList typeNodes = matrixElement.getElementsByTagName("Type");
                    if (typeNodes.getLength() > 0) {
                        Element typeElement = (Element) typeNodes.item(0);
                        prop.setType(typeElement.getTextContent());
                    } else {
                        prop.setType("");
                    }
                    PropInfo propInfo = new PropInfo();
                    propInfo.setProp(prop);
                    propInfoList.getPropInfoList().add(propInfo);
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "Error parsing XML file", ex);
            }
        }
        return propInfoList;
    }
}
